package org.jeecg.modules.yfJiaofgl.Job;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.caiGgl.entity.HqCggl;
import org.jeecg.modules.yfJiaofgl.entity.HqYfJiaofgl;
import org.jeecg.modules.yfJiaofgl.service.IHqYfJiaofglService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
public class syncYingfjf implements Job {
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        log.info(String.format("定时扫描营房缴费数据，任务执行了，时间:" + DateUtils.formatDateTime()));
        syncYingfjf();
    }


    @Autowired
    RestTemplate restTemplate;
    @Autowired
    private IHqYfJiaofglService hqYfJiaofglService;

    @Value("${yingfjf.viewURL}")
    String viewURL ;

    @Value("${yingfjf.userUrl}")
    String userUrl ;

    void syncYingfjf(){
       // String userName="";
        String messageContent="您已欠费，请缴费";



        Map <String, List <HqYfJiaofgl>> hqYfJiaofgls = hqYfJiaofglService.list(new QueryWrapper <HqYfJiaofgl>()
                                                            .eq("jiaofzt","2")).parallelStream().collect(Collectors.groupingBy(HqYfJiaofgl::getYez));

        if (hqYfJiaofgls!=null) {

            hqYfJiaofgls.forEach((k, v) -> {

                String  userName=v.get(0).getYez();
                Integer  tixcs= v.get(0).getTixcs();
                String jiaoflx=v.get(0).getJiaoflx();
                HqYfJiaofgl hqYfJiaofgl=hqYfJiaofglService.getById(v.get(0).getId());

                tixcs =tixcs+1;
                hqYfJiaofgl.setTixcs(tixcs);
                hqYfJiaofglService.updateById(hqYfJiaofgl);

                Map<String, Object> map = new HashMap<>();
                map.put("userName", userName);
                map.put("messageContent", messageContent);
                map.put("viewURL", viewURL);

             //   String userUrl="http://26.29.23.62:8080/api/jsonws/FeedBack-portlet.feedback/addNotification";
                String jsonString = JSON.toJSONString(map);
                userUrl=userUrl+"?jsonParam="+jsonString;





//            JSONObject jsonObject = restTemplate.getForObject(userUrl, JSONObject.class);

                String jsonObject = restTemplate.getForObject(userUrl, String.class);
                log.info(jsonObject.toString());
            });

        }


    }


}
